

//set scheme s1mono // monochrome 
use "$data/tech_cell_input.dta", clear

keep if cells_50km_stateborders

drop if year == 1996
	
egen t = group(year)
xtset id t
	
	forval i=1/4{
		g lang_control_rX`i' = lang_control*(t==`i')
		g share_off_speakers_rX`i' = share_off_speakers*(t==`i')
	}
	
replace lang_control_rX2 = 0
replace share_off_speakers_rX2 = 0

** Regression w/o conley 
reghdfe hyv_share lang_control_rX* share_off_speakers_rX*  if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id)

gen in_sample = e(sample)


foreach var in hyv_share lang_control_rX1 lang_control_rX2 lang_control_rX3 lang_control_rX4 {
	
	reghdfe `var' share_off_speakers_rX*  if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. & in_sample == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id) residuals(ralt_`var')
		
	replace ralt_`var' = sqrt(pop2001) * ralt_`var'
}

foreach var in hyv_share {

ols_spatial_HAC ralt_`var' ralt_lang_control_rX1 ralt_lang_control_rX2 ralt_lang_control_rX3 ralt_lang_control_rX4 if cells_50km_stateborders == 1 , lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(10) bartlett dropvar

regsave using "$proc/94_`var'_10.dta", replace 

}

************* CALLS 

	use "$data/calls_cell_input.dta", clear	
	
	keep if cells_50km_stateborders == 1
	
		foreach var in calls_all_per100C calls_tech_per100C calls_others_per100C {
				bysort id (year) : gen cum_`var' = sum(`var')
			}
		foreach var in calls_all_per100C calls_tech_per100C calls_others_per100C {
				replace `var' = cum_`var' if year == 2010
				replace `var' = 0 if year == 2009
			}
		****
		drop if year < 2009 | year > 2017
		egen t = group(year)
		
		forval i=1/9{
			g lang_control_rX`i' = lang_control*(t==`i')
			g share_off_speakers_rX`i' = share_off_speakers*(t==`i')
		}
		
		// set baseline as 2009 (t = 1)
		replace lang_control_rX1 = 0
		replace share_off_speakers_rX1 = 0

reghdfe calls_all_per100C lang_control_rX* share_off_speakers_rX*  if cells_50km_stateborders == 1, a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id)

gen in_sample = e(sample)

foreach var in calls_all_per100C calls_tech_per100C calls_others_per100C ///
lang_control_rX1 lang_control_rX2 lang_control_rX3 lang_control_rX4 lang_control_rX5 lang_control_rX6 lang_control_rX7 lang_control_rX8 lang_control_rX9 {
	reghdfe `var' share_off_speakers_rX*  if cells_50km_stateborders == 1 & in_sample == 1 , a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id) residuals(ralt_`var')
}

local var calls_all_per100C
	
local dist = 10 

ols_spatial_HAC ralt_`var' ralt_lang_control_rX1 ralt_lang_control_rX2 ralt_lang_control_rX3 ralt_lang_control_rX4 ralt_lang_control_rX5 ralt_lang_control_rX6 ralt_lang_control_rX7 ralt_lang_control_rX8 ralt_lang_control_rX9 if cells_50km_stateborders == 1 , lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') bartlett dropvar

regsave using "$proc/94_`var'_`dist'.dta", replace 

************** YIELD 

use "$data/yield_cell_input.dta", clear

keep if cells_50km_stateborders == 1

			drop if year < 2002 
			gen t = (year - 2002) + 1

			forval i=1/16{
			g lang_control_rX`i' = lang_control*(t==`i')
			g share_off_speakers_rX`i' = share_off_speakers*(t==`i')
		}
		
		
		replace lang_control_rX6 = 0
		replace share_off_speakers_rX6 = 0
		

reghdfe total_yield_idt  lang_control_rX* share_off_speakers_rX* if cells_50km_stateborders == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id) 
gen in_sample = e(sample)

**  
foreach var in total_yield_idt lang_control_rX1 lang_control_rX2 lang_control_rX3 lang_control_rX4 lang_control_rX5 lang_control_rX6 lang_control_rX7 lang_control_rX8 lang_control_rX9 lang_control_rX10 lang_control_rX11 lang_control_rX12 lang_control_rX13 lang_control_rX14 lang_control_rX15 lang_control_rX16 {
	
	reghdfe `var' share_off_speakers_rX*  if cells_50km_stateborders == 1 & in_sample == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id) residuals(ralt_`var')
		
	replace ralt_`var' = sqrt(pop2001) * ralt_`var'
}


foreach var in total_yield_idt {

ols_spatial_HAC ralt_`var' ralt_lang_control_rX1 ralt_lang_control_rX2 ralt_lang_control_rX3 ralt_lang_control_rX4 ralt_lang_control_rX5 ralt_lang_control_rX6 ralt_lang_control_rX7 ralt_lang_control_rX8 ralt_lang_control_rX9 ralt_lang_control_rX10 ralt_lang_control_rX11 ralt_lang_control_rX12 ralt_lang_control_rX13 ralt_lang_control_rX14 ralt_lang_control_rX15 ralt_lang_control_rX16 if cells_50km_stateborders == 1 & in_sample == 1 , lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(10) bartlett dropvar

regsave using "$proc/94_`var'_10.dta", replace 

}


** Graphing these event studies 
** Calls 

local dist = 10
	
use "$proc/94_calls_all_per100C_`dist'.dta",clear

gen period = substr(var, 21, 21)
	destring period, replace

insobs 1 
replace period = 1 if missing(period)

replace coef = 0 if missing(coef)
replace stderr = 0 if missing(stderr)

gen year = 2009 + (period-1)
	
	 gen t_10=1.65
	 gen t_5=1.96
	 
	 foreach num in 5 10 {
	 gen low_bound_`num'=coef-(t_`num'*stderr)
	 gen up_bound_`num'=coef+(t_`num'*stderr)
	}
	

	
	twoway (rbar up_bound_10 low_bound_10 year ,fi(inten40) barwidth(0.2) lcolor(gs11) color(gs7)) (rcap up_bound_5 low_bound_5 year, lcolor(black) lstyle(ci)) /*(rcap up_bound_10 low_bound_10 year, lcolor(red) lstyle(ci))*/  (scatter coef year, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
	legend(rows(1) label(1 "90% CI") label(2 "95% CI") label(3 "Point estimates") size(12pt)) graphregion(color(white)) ylab(-15(5)5,nogrid) xlab(2009(2)2017) yline(0, lcolor(black)) ///
	xtitle("Year") 
				
	graph export "$results/graphs/figure_2a.pdf", as(pdf) replace 


** Tech adoption
use "$proc/94_hyv_share_10.dta",clear

	gen period = substr(var, 21, 21)
	destring period, replace 
	
	insobs 1 
	replace period = 2 if missing(period)
	
	replace coef = 0 if missing(coef)
	replace stderr = 0 if missing(stderr )

	 gen year = 2002 + 5*(period-1)
	
	  gen t_10=1.65
	  gen t_5=1.96
	  
	  
	  foreach num in 5 10 {
	  gen low_bound_`num'=coef-(t_`num'*stderr)
	  gen up_bound_`num'=coef+(t_`num'*stderr)
	 }
	 

	 
	twoway (rbar up_bound_10 low_bound_10 year ,fi(inten40) barwidth(0.2) lcolor(gs11) color(gs7)) (rcap up_bound_5 low_bound_5 year, lcolor(black) lstyle(ci))  (scatter coef year, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
	legend(rows(1) label(1 "90% CI") label(2 "95% CI") label(3 "Point estimates") size(12pt)) graphregion(color(white)) ylab(-0.04(0.02)0.02,nogrid) xlab(2002(5)2017) yline(0, lcolor(black)) ///
	xtitle("Year")  
				
	graph export "$results/graphs/figure_2b.pdf", as(pdf) replace 

** Yield 
use "$proc/94_total_yield_idt_10.dta", clear 

gen period = substr(var, 21, 21)
destring period, replace 
	
insobs 1 
replace period = 6 if missing(period)

replace coef = 0 if missing(coef)
replace stderr = 0 if missing(stderr)

gen year = 2002 + (period-1)

gen t_10=1.65
gen t_5=1.96
	  
	  
 foreach num in 5 10 {
	  gen low_bound_`num'=coef-(t_`num'*stderr)
	  gen up_bound_`num'=coef+(t_`num'*stderr)
}

twoway (rbar up_bound_10 low_bound_10 year ,fi(inten40) barwidth(0.2) lcolor(gs11) color(gs7)) (rcap up_bound_5 low_bound_5 year, lcolor(black) lstyle(ci))  (scatter coef year, lcolor(black) mcolor(black) msymbol(circle) msize(medium)) , ///
				graphregion(color(white)) ylab(-0.05(0.01)0.03,nogrid) xlab(2002(2)2017) yline(0, lcolor(black)) ///
				xtitle("Year") ///
				legend(rows(1) label(1 "90% CI") label(2 "95% CI") label(3 "Point estimates") size(12pt))
				

graph export "$results/graphs/figure_2c.pdf", as(pdf) replace 

erase "$proc/94_calls_all_per100C_10.dta"
erase "$proc/94_hyv_share_10.dta"
erase "$proc/94_total_yield_idt_10.dta"
